﻿using LeanApp.Bll;
using LeanApp.Common;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace LeanApp.View
{
    /// <summary>
    /// SpcCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class SpcCtrl : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public SpcCtrl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 解析所得的数据
        /// </summary>
        private List<Dictionary<string, object>> _thisData = new List<Dictionary<string, object>>();

        /// <summary>
        /// 文件打开的单击事件
        /// </summary>
        /// <param name="sender">对象</param>
        /// <param name="e">事件参数</param>
        private void BtnOpenFile_Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Title = "请打开要执行SPC分析的文件";
                openFileDialog.Filter = "Excel文件|*.xlsx|Excel文件|*.xls";
                if (openFileDialog.ShowDialog() == true)
                {
                    string fileName = openFileDialog.FileName;
                    _thisData = ExcelTool.ReadExcel(fileName);
                    if (_thisData.Count <= 0 || _thisData[0] == null || _thisData[0].Count <= 0)
                    {
                        MessageBox.Show("数据读取失败");
                    }
                    else
                    {
                        int rowCount = _thisData.Count;
                        int colCount = _thisData[0].Count;
                        List<string> colNames = _thisData[0].Keys.ToList();
                        colNames.Remove("id");
                        txtExcelBrief.Text = $@"读入：{rowCount}行，{colCount}列";
                        cmbTarget.Items.Clear();
                        colNames.ForEach(a => cmbTarget.Items.Add(a));
                        cmbTarget.SelectedIndex = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 开始分析的单击事件
        /// </summary>
        /// <param name="sender">对象</param>
        /// <param name="e">事件参数</param>
        private void BtnStart_Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string selectedTarget = cmbTarget.SelectedItem.ToString();
                if (string.IsNullOrWhiteSpace(selectedTarget))
                {
                    MessageBox.Show("没有选定正常的目标");
                    return;
                }
                if (_thisData.Count < 25)
                {
                    MessageBox.Show("样本数小于25，无法进行spc分析");
                    return;
                }
                List<double> valList = new List<double>();
                foreach (Dictionary<string, object> item in _thisData)
                {
                    if (!(item[selectedTarget] is double))
                    {
                        MessageBox.Show("样本中存在无法计算的数值");
                        return;
                    }
                    valList.Add(Convert.ToDouble(item[selectedTarget]));
                }
                if (valList.Count > 0)
                {
                    bool tmp = double.TryParse(txtTarget.Text, out double specVal);
                    if (!tmp)
                    {
                        MessageBox.Show("规格输入错误");
                        return;
                    }
                    tmp = double.TryParse(txtUpLimit.Text, out double uDiff);
                    if (!tmp)
                    {
                        MessageBox.Show("上偏差输入错误");
                        return;
                    }
                    tmp = double.TryParse(txtLowLimit.Text, out double lDiff);
                    if (!tmp)
                    {
                        MessageBox.Show("下偏差输入错误");
                        return;
                    }
                    SpcBll spcBll = new SpcBll();
                    spcBll.Source = valList;
                    spcBll.SpecVal = specVal;
                    spcBll.LowerDiff = lDiff;
                    spcBll.UpperDiff = uDiff;
                    txtCPkValue.Visibility = Visibility.Visible;
                    txtCPkValue.Text = $@"CPK：{spcBll.Cpk.ToString("0.0000")}";
                    txtCPkResult.Visibility = Visibility.Visible;
                    txtCPkResult.Text = $@"结果：{spcBll.Result}";
                    txtCPkResult.Foreground = spcBll.ResultBrush;
                    ControlCtrl controlCtrl = new ControlCtrl("SPC控制图", spcBll);
                    if (controlCtrlContariner.Children.Count > 0)
                    {
                        controlCtrlContariner.Children.Clear();
                    }
                    controlCtrlContariner.Children.Add(controlCtrl);

                    QuartileCtrl quartileCtrl = new QuartileCtrl("四分位图", spcBll);
                    if (quartileCtrlContariner.Children.Count > 0)
                    {
                        quartileCtrlContariner.Children.Clear();
                    }
                    quartileCtrlContariner.Children.Add(quartileCtrl);

                    CapabilityCtrl capabilityCtrl = new CapabilityCtrl("能力图", spcBll);
                    if (capabilityCtrlContariner.Children.Count > 0)
                    {
                        capabilityCtrlContariner.Children.Clear();
                    }
                    capabilityCtrlContariner.Children.Add(capabilityCtrl);

                    //groupDataCtrlContariner
                    GroupInfoCtrl groupInfoCtrl = new GroupInfoCtrl("分组情况", spcBll);
                    if (groupDataCtrlContariner.Children.Count > 0)
                    {
                        groupDataCtrlContariner.Children.Clear();
                    }
                    groupDataCtrlContariner.Children.Add(groupInfoCtrl);

                    //groupChartCtrlContariner
                    ProbabilityCtrl probabilityCtrl = new ProbabilityCtrl("概率分布直方图", spcBll);
                    if (groupChartCtrlContariner.Children.Count > 0)
                    {
                        groupChartCtrlContariner.Children.Clear();
                    }
                    groupChartCtrlContariner.Children.Add(probabilityCtrl);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}